using Microsoft.AspNetCore.Mvc;
using ZR.Model.Business.Dto;
using ZR.Model.Business;
using ZR.Service.Business.IBusinessService;

//创建时间：2025-08-06
namespace ZR.Admin.WebApi.Controllers.Business
{
    /// <summary>
    /// 用户表
    /// </summary>
    [Route("business/User")]
    public class UserController : BaseController
    {
        /// <summary>
        /// 用户表接口
        /// </summary>
        private readonly IUserService _UserService;

        public UserController(IUserService UserService)
        {
            _UserService = UserService;
        }

        /// <summary>
        /// 查询用户表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "user:list")]
        public IActionResult QueryUser([FromQuery] UserQueryDto parm)
        {
            var response = _UserService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询用户表详情
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet("{UserId}")]
        [ActionPermissionFilter(Permission = "user:query")]
        public IActionResult GetUser(long UserId)
        {
            var response = _UserService.GetInfo(UserId);
            
            var info = response.Adapt<UserDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加用户表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "user:add")]
        [Log(Title = "用户表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddUser([FromBody] UserDto parm)
        {
            var modal = parm.Adapt<User>().ToCreate(HttpContext);

            var response = _UserService.AddUser(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新用户表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "user:edit")]
        [Log(Title = "用户表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateUser([FromBody] UserDto parm)
        {
            var modal = parm.Adapt<User>().ToUpdate(HttpContext);
            var response = _UserService.UpdateUser(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除用户表
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "user:delete")]
        [Log(Title = "用户表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteUser([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_UserService.Delete(idArr));
        }

    }
}